<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        @click="handel(null)"
        :class="[obj.sorter == null ? 'active' : '']"
        href="javascript:;"
        >推荐</a
      >
      <a
        @click="handel('weight_desc')"
        :class="[obj.sorter == 'weight_desc' ? 'active' : '']"
        href="javascript:;"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="到底了宝子"
        @load="onLoad"
        offset="100"
      >
        <!-- .native 为什么要添加
        1.组件身上是没有标签元素提供事件的
        2.乳沟写了有事件,这个只能是子组件传过来的  this.$emit()
        3.如果想让组件也具备有标签元素事件  需要添加native修饰符 作用:触发其他原生事件
      -->
        <!-- <ArticleItem
          v-for="item in list"
          :key="item.id"
          :item="item"
          @click.native="goDetail(item.id)"     1
            @click="goDetail(item.id)"    2
        > -->
        <ArticleItem v-for="item in list" :key="item.id" :item="item">
        </ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { ArticleAPI } from '@/api/article.js'
export default {
  name: 'article-page',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      // pagesize: 20,
      // pagenum: 1,
      refreshing: false,
      obj: {
        current: 1,
        pageSize: 10,
        sorter: null
      }
    }
  },
  created () {
    // this.getArticleList()
  },
  methods: {
    goDetail (id) {
      // console.log(id, 7890)

    },
    handel (sorter) {
      this.obj.sorter = sorter
      this.refreshing = true
      this.onLoad()
    },
    onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1);
      //   }

      //   // 加载状态结束
      //   this.loading = false;

      //   // 数据全部加载完成
      //   if (this.list.length >= 40) {
      //     this.finished = true;
      //   }
      // }, 1000);
      setTimeout(() => {
        if (this.obj.current > 3) {
          this.loading = false;//加载最后一次要把loading 状态变成false
          this.finished = true;
          return
        }
        this.getArticleList()
        this.obj.current += 1
      }, 1000)

    },
    async getArticleList () {
      // const res = await ArticleAPI(this.obj).catch(err=>err)
      // if(res.code == 10000){
      //   this.list = res.data.rows
      // }
      try {
        const res = await ArticleAPI(this.obj)
        // console.log(res)
        this.list = this.refreshing ? res.data.rows : [...this.list, ...res.data.rows]
        //  异步写在 onload里面会直接全部加载
        this.loading = false;
        this.refreshing = false
      } catch (error) {

      }
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      // this.loading = true;
      this.obj.current = 1
      this.onLoad();
    },
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
